package com.itheima.service;

import com.itheima.model.dao.CustomerDao;
import com.itheima.model.dao.RuleDao;
import com.itheima.model.Customer;
import com.itheima.model.Rule;
import com.itheima.model.VipRule;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class DiscoutService {
    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private RuleDao ruleDao;

    public Customer getCustomer(String idCard){
        return customerDao.findByIdCard(idCard);
    }
    public Object getVipLevel(Object o){
        return o;
    }
    public BigDecimal discount(String vipLevel,String itemName,BigDecimal price) throws Exception {
        VipRule vipRule = ruleDao.findVipRuleByLevel(vipLevel);
        List<Rule> ruleList = ruleDao.findRulesByVipRuleId((long) vipRule.getId());
        for (Rule rule : ruleList){
            ExpressRunner runner = new ExpressRunner();
            DefaultContext<String,Object> context = new DefaultContext<String, Object>();
            String express = rule.getExpress();
            context.put("价格",price);
            Object r = runner.execute(express,context,null,true,false);
            return BigDecimal.valueOf(Double.valueOf(String.valueOf(r)));
        }
        return null;
    }
}
